
/**
* @Title: SearchController.java
* @Package com.leetone.search.controller
* @Description: 【】
* @date 2018年4月5日 上午8:53:25
* @version V1.0
* @Company: 上海
* @author:[谭磊][leetone123@163.com]
*/

package com.leetone.search.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.leetone.search.pojo.SearchResult;
import com.leetone.search.service.SearchService;
import com.leetone.utils.ExceptionUtils;
import com.leetone.utils.TaotaoResult;

/**
* @ClassName: SearchController
* @Description: 【商品查询】
* @date 2018年4月5日 上午8:53:25
* @Company: 上海
* @author:[谭磊][leetone123@163.com]
*/
@Controller
public class SearchController {
	@Autowired
	private SearchService searchService;
	@RequestMapping("/query")
	@ResponseBody
	public TaotaoResult search(@RequestParam("q")String queryString,
			@RequestParam(defaultValue="1")Integer page,
			@RequestParam(defaultValue="20")Integer rows)
	{
		//查询条件不能为空
		if(StringUtils.isBlank(queryString))
		{
			return TaotaoResult.build(400, "查询条件不能为空！");
		}
		SearchResult result =null;
		try {
			//解决参数乱码
			queryString = new String(queryString.getBytes("ISO8859-1"),"UTF-8");
			result = searchService.search(queryString, page, rows);
		} catch (Exception e) {
			e.printStackTrace();
			return TaotaoResult.build(500, ExceptionUtils.getStackTrace(e));
		}
		return TaotaoResult.ok(result);
	}
}
